﻿var InsertCalculatorHotel = function (hotelTypeItem, nationalItem, cityItem, hotelItem, tourItem, roomItem,
    dateBeginItem, dateEndItem, quantityAdultItem, quantityChildItem, priceAdultItem, priceChildItem,
    vatItem, totalPriceHotel, countNightItem , seasonTour, tourInfo,priceBabyItem, quantityBabyItem  ) {



    var loadPriceRoom = function () { 
        var roomId = $(roomItem).val(); 
        $.ajax({
            type: "POST",
            url: "/ajax_mh.html",
            data: { "function": "GetPrice", "priceId": roomId },
            dataType: "json",
            success: function (data) { 
                if (data && data.isError == false) {
                    $(priceAdultItem).val(data.PriceAdult);
                    $(priceChildItem).val(data.PriceChild);
                    $(priceBabyItem).val(data.PriceBaby);
                }
            }
        });
    };
    $(roomItem).change(loadPriceRoom);
    //ham them sua xoa
    var fncCalculatorHotel = function() {

        var typeId = 5;
        var tourId = parseInt(tourItem);
        //room =PriceId
        var roomId = $(roomItem).val();
        var dateBegin = $(dateBeginItem).val();
        var dateEnd = $(dateEndItem).val();
        var quantityAdult = $(quantityAdultItem).val();
        var quantityChild = $(quantityChildItem).val();
        var quantityBaby = $(quantityBabyItem).val();
        var priceAdult = $(priceAdultItem).val();
        var priceChild = $(priceChildItem).val();
        var priceBaby = $(priceBabyItem).val();
        var vat = $(vatItem).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
     
       
            $.ajax({
                type: "POST",
                url: "/ajax_mh.html",
                data: { "function": "InsertCalculatorHotel", "TypeId": typeId, "TourId": tourId, "RoomId": roomId, "DateBegin": dateBegin, "DateEnd": dateEnd, "QuantityAdult": quantityAdult, "QuantityChild": quantityChild, "PriceAdult": priceAdult, "PriceChild": priceChild, "VAT": vat, "CalculatorId": calculatorId, "ClassName": className, "PriceBaby": priceBaby, "QuantityBaby": quantityBaby },
                dataType: "json",
                success: function(data) {
                    $(".ListHotel").remove();
                    $(hotelTypeItem).val('-1');
                    $(nationalItem).val('-1');
                    $(cityItem).val('-1');
                    $(roomItem).val('-1');
                    $(dateBeginItem).val('');
                    $(dateEndItem).val('');
                    $(quantityAdultItem).val('');
                    $(quantityChildItem).val('');
                    $(quantityBabyItem).val('');
                    $(priceAdultItem).val('');
                    $(priceChildItem).val('');
                    $(priceBabyItem).val('');
                    $(vatItem).val('');
                    $(countNightItem).text('');
                    $(totalPriceHotel).text('');
                    if (data && data.isError == false) {
                        $.each(data.array, function(index, item) {
                            $('#new-ticket-Hotel')
                                .before($("<tr class='ListHotel' id='ticket-hotel-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                    '<td >' + item.HotelType + '</td>' +
                                    '<td >' + item.National + '</td>' +
                                    '<td >' + item.City + '</td>' +
                                    '<td >' + item.Hotel + '</td>' +
                                    '<td >' + item.DateBegin + '</td>' +
                                    '<td >' + item.DateEnd + '</td>' +
                                    '<td>' + item.CountNight + '</td>' +
                                    '<td >' + item.RoomType + '</td>' +
                                    '<td >' + item.PriceAdult + '</td>' +
                                    '<td >' + item.PriceChild + '</td>' +
                                    '<td >' + item.PriceBaby + '</td>' +
                                    '<td >' + item.QuantityAdult + '</td>' +
                                    '<td >' + item.QuantityChild + '</td>' +
                                    '<td >' + item.QuantityBaby + '</td>' +
                                    '<td >' + item.VAT + '</td>' +
                                    '<td>' + item.Total + '</td>' +
                                        '<td></td>' +
                                       '<td>' +
                                    '<input class="btnUpdateHotel"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                    '<input class="btnDeleteHotel"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                    '</td>'));
                        });
                        $('#TotalTicketHotelPrices').html(data.TotalTicketHotelPrices);
                        $('#TotalTicketHotelPricesAdults').html(data.TotalTicketHotelPricesAdults);
                        $('#TotalTicketHotelPricesChild').html(data.TotalTicketHotelPricesChild);
                        $('#TotalTicketHotelPricesBaby').html(data.TotalTicketHotelPricesBaby);

                        //$('#TotalTicketHotelPrices').html(data.TotalTicketHotelPrices);
                        //$('#TotalTicketHotelPricesAdults').html(data.TotalTicketHotelPricesAdults);
                        //$('#TotalTicketHotelPricesChild').html(data.TotalTicketHotelPricesChild);
                        //$('#TotalTicketHotelPricesBaby').html(data.TotalTicketHotelPricesBaby);

                        $('#btnInsertHotel').attr("calculatorid", "-1").val("Thêm");
                        $('.btnUpdateHotel').show();
                        $('#btnDeleteHotel').hide();
                    }
                }
            });
         
    };

    $('#btnInsertHotel').live("click", fncCalculatorHotel);
            $('.btnDeleteHotel').live("click", fncCalculatorHotel);
            $('.btnUpdateHotel').live('click', function() {
                var calculatorId = $(this).attr("calculatorid");

                $.ajax({
                    type: "POST",
                    url: "/ajax_mh.html",
                    data: { "function": "UpdateCalculatorHotel", "CalculatorId": calculatorId },
                    dataType: "json",
                    success: function(data) {
                        $('#ticket-hotel-' + calculatorId).remove();
                        $(hotelTypeItem).val(data.HotelTypeId);
                        $(nationalItem).val(data.Nationalid);
                        loadCityNationalid(data.CityId, data.HotelTypeId, data.HotelId, data.PriceId, seasonTour);
                        $(dateBeginItem).val(data.BeginDate);
                        $(dateEndItem).val(data.EndDate);
                        $(quantityAdultItem).val(data.QuantityAdults);
                        $(quantityChildItem).val(data.QuantityChild);
                        $(quantityBabyItem).val(data.QuantityBaby);
                        $(priceAdultItem).val(data.PriceAdults);
                        $(priceChildItem).val(data.PriceChild);
                        $(priceBabyItem).val(data.PriceBaby);
                        $(vatItem).val(data.Vat);
                        $(countNightItem).text(fncCountNightauto(data.BeginDate, data.EndDate));
                        $(totalPriceHotel).text(data.TotalPriceHotel);
                        $('#btnInsertHotel').attr("calculatorid", calculatorId).val("Lưu");
                        $('.btnUpdateHotel').hide();
                        $('#btnDeleteHotel').show();
                    }
                });
            });
            //load city
            var loadCityNationalid = function(cityItemId, hotelTypeId, hotelId, priceId, season) {
                var nationalId = $(nationalItem).val();
                $.ajax({
                    type: "POST",
                    url: "/ajax_mh.html",
                    data: { "function": "ListCityByNational", "NationalId": nationalId },
                    dataType: "json",
                    success: function(data) {
                        $(cityItem).html('<option value="-1">--- Chọn tỉnh/thành phố ---</option>');
                        if (data && data.isError == false) {
                            $.each(data.array, function(index, item) {
                                $(cityItem)
                                    .append($("<option></option>")
                                        .attr("value", item.ID)
                                        .text(item.Name));
                            });
                            $(cityItem).val(cityItemId);
                            fncHotel(hotelId, priceId, season);
                        }
                    }
                });
            };

            $(nationalItem).change(loadCityNationalid);
            //load Hotel
            var fncHotel = function(hotelId, priceId) {
                var hotelType = $(hotelTypeItem).val();
                var city = $(cityItem).val();
                $.ajax({
                    type: "POST",
                    url: "/ajax_mh.html",
                    data: { "function": "ListHotelByHotelType", "HotelTypeId": hotelType, "CityId": city, "Item_Type": 5 },
                    dataType: "json",
                    success: function(data) {
                        $(hotelItem).html('<option value="-1">--- Chọn khách sạn---</option>');
                        if (data && data.isError == false) {
                            $.each(data.array, function(index, item) {
                                $(hotelItem)
                                    .append($("<option></option>")
                                        .attr("value", item.ID)
                                        .text(item.Name));
                            });
                            $(hotelItem).val(hotelId);
                            loadRoomTypeByHotel(priceId);
                        }
                    }
                });
            };
            $(hotelTypeItem).change(fncHotel);
            $(cityItem).change(fncHotel);
            //Load loai
            var loadRoomTypeByHotel = function(priceId) {
                var hotelId = $(hotelItem).val();
                var season = seasonTour;
                $.ajax({
                    type: "POST",
                    url: "/ajax_mh.html",
                    data: { "function": "ListRoomTypeByHotel", "HotelId": hotelId, "Season": season },
                    dataType: "json",
                    success: function(data) {
                        $(roomItem).html('<option value="-1">--- Chọn loại phòng---</option>');
                        if (data && data.isError == false) {
                            $.each(data.array, function(index, item) {
                                $(roomItem)
                                    .append($("<option></option>")
                                        .attr("value", item.ID)
                                        .text(item.Name));
                            });
                            $(roomItem).val(priceId);
                        }
                    }
                });
            };
            $(hotelItem).change(loadRoomTypeByHotel);

            var diffDays = 0;
            var fncCountNight = function() {
                if ($(dateBeginItem).val() != "" && $(dateEndItem).val() != "") {
                    var dateBegin = $(dateBeginItem).val().split("/");
                    var dateEnd = $(dateEndItem).val().split("/");
                    var firstDate = new Date(dateBegin[2], dateBegin[1], dateBegin[0]);
                    var secondDate = new Date(dateEnd[2], dateEnd[1], dateEnd[0]);
                    var oneDay = 24 * 60 * 60 * 1000;
                    diffDays = Math.abs((firstDate.getTime() - secondDate.getTime()) / oneDay);
                    if (diffDays == 0) {
                        diffDays = 1;
                    }
                    $(countNightItem).text(diffDays);
                }
            };
            $(dateBeginItem).change(fncCountNight);
            $(dateEndItem).change(fncCountNight);
            var fncCountNightauto = function(dateBeginauto, dateEndauto) {
                if ($(dateBeginItem).val() != "" && $(dateEndItem).val() != "") {
                    var dateBegin = dateBeginauto.split("/");
                    var dateEnd = dateEndauto.split("/");
                    var firstDate = new Date(dateBegin[2], dateBegin[1], dateBegin[0]);
                    var secondDate = new Date(dateEnd[2], dateEnd[1], dateEnd[0]);
                    var oneDay = 24 * 60 * 60 * 1000;
                    diffDays = Math.abs((firstDate.getTime() - secondDate.getTime()) / oneDay);
                    if (diffDays == 0) {
                        diffDays = 1;
                    }
                    $(countNightItem).text(diffDays);
                }
            };
            var fncTotalPriceRoom = function() {

                var quantityAdult = $(quantityAdultItem).val();
                var quantityChild = $(quantityChildItem).val();
                var quantityBaby = $(quantityBabyItem).val();
                var priceAdult = $(priceAdultItem).val();
                var priceChild = $(priceChildItem).val();
                var priceBaby = $(priceBabyItem).val();
                var vat = $(vatItem).val();
                if (vat.toString() == NaN) vat = 0;
                var total = ((quantityAdult * priceAdult +
                        quantityChild * priceChild + quantityBaby * priceBaby) +
                    ((quantityAdult * priceAdult + quantityChild * priceChild + quantityBaby * priceBaby) * vat) / 100) * diffDays;
                $(totalPriceHotel).text(total);
            };
            $(quantityAdultItem).keyup(fncTotalPriceRoom);
            $(quantityChildItem).keyup(fncTotalPriceRoom);
            $(quantityBabyItem).keyup(fncTotalPriceRoom);
            $(priceAdultItem).keyup(fncTotalPriceRoom);
            $(priceBabyItem).keyup(fncTotalPriceRoom);
            $(priceChildItem).keyup(fncTotalPriceRoom);
            $(vatItem).keyup(fncTotalPriceRoom);
            $(dateBeginItem).change(fncTotalPriceRoom);
            $(dateEndItem).change(fncTotalPriceRoom);
        
    };
    
    